var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var goodsTypeRouter = require('./routes/goodsType');
var imagesRouter = require('./routes/images')
var goods = require('./routes/goods')
var banner=require('./routes/banner')

var app = express();


// 连接数据库
const mongoose = require('mongoose');
// 设置数据库服务器地址
const dbURI = 'mongodb://admin:Du707294@101.43.135.107:27017/osnode?authSource=admin';
// const dbURI = 'mongodb://127.0.0.1/mynode';
// 连接数据库
mongoose.connect(dbURI, { useNewUrlParser: true, useUnifiedTopology: true });
// 当数据库连接成功时触发该事件
mongoose.connection.on('connected', () => console.log(dbURI + ' 数据库连接成功'));
//使用token规则
const jwtAuth = require('./utils/jwt');
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/goodsType', goodsTypeRouter);
app.use('/image',imagesRouter)
app.use('/goods',goods)
app.use('/banner',banner)



// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
app.listen(3001,()=>{console.log('3001服务器启动成功！')})
